<template>
  <view class="container">
    <view class="content">
      <!-- 问卷列表 -->
      <view class="survey-list">
        <view class="empty-tip" v-if="surveyList.length === 0 && !loading">
          <text>暂无可用问卷</text>
        </view>
        <view v-else v-for="(item, index) in surveyList" :key="index" class="survey-item">
          <view class="survey-card">
            <view class="survey-info">
              <text class="survey-title">{{ item.title }}</text>
              <text class="survey-desc">{{ item.description }}</text>
              <view class="survey-meta">
                <text class="question-count">{{ item.questionCount || '0' }}个问题</text>
                <text class="time-estimate">预计{{ item.timeEstimate || '5' }}分钟</text>
              </view>
            </view>
            <view class="survey-actions">
              <button class="btn-primary" @click="startSurvey(item)">开始填写</button>
            </view>
          </view>
        </view>
        
        <!-- 加载更多 -->
        <view class="loading-more" v-if="loading">
          <view class="loading-content">
            <text class="loading-text">加载中...</text>
          </view>
        </view>
      </view>
    </view>

    <!-- 底部导航栏 -->
    <view class="tab-bar">
      <view class="tab-item" @click="handleTabClick('home')">
        <text class="tab-icon">🏠</text>
        <text class="tab-text">首页</text>
      </view>
      <view class="tab-item active" @click="handleTabClick('survey')">
        <text class="tab-icon" style="color: #2979ff;">📋</text>
        <text class="tab-text" style="color: #2979ff;">问卷</text>
      </view>
      <view class="tab-item" @click="handleTabClick('record')">
        <text class="tab-icon">✅</text>
        <text class="tab-text">记录</text>
      </view>
      <view class="tab-item" @click="handleTabClick('knowledge')">
        <text class="tab-icon">📚</text>
        <text class="tab-text">知识</text>
      </view>
      <view class="tab-item" @click="handleTabClick('my')">
        <text class="tab-icon">👤</text>
        <text class="tab-text">我的</text>
      </view>
    </view>
  </view>
</template>

<script>
import { ref, onMounted } from 'vue';

export default {
  name: 'Survey',
  setup() {
    const surveyList = ref([
      {
        id: 1,
        title: '睡眠质量评估问卷',
        description: '通过回答一系列问题评估您的睡眠质量',
        questionCount: 10,
        timeEstimate: '5'
      },
      {
        id: 2,
        title: '失眠严重程度评估',
        description: '评估您的失眠严重程度，并提供改善建议',
        questionCount: 8,
        timeEstimate: '4'
      },
      {
        id: 3,
        title: '睡眠习惯调查',
        description: '了解您的睡眠习惯，帮助改善睡眠质量',
        questionCount: 12,
        timeEstimate: '6'
      }
    ]);
    
    const loading = ref(false);
    const loadingText = ref({
      contentdown: '上拉显示更多',
      contentrefresh: '加载中...',
      contentnomore: '没有更多数据了'
    });
    
    // 开始问卷
    const startSurvey = (item) => {
      uni.navigateTo({
        url: `/pages/survey/detail?id=${item.id}`
      });
    };
    
    // 处理标签点击
    const handleTabClick = (tab) => {
      switch (tab) {
        case 'home':
          wx.navigateTo({ url: '/pages/index/index' });
          break;
        case 'survey':
          // 当前已在问卷页，不需要跳转
          break;
        case 'record':
          wx.navigateTo({ url: '/pages/record/index' });
          break;
        case 'knowledge':
          wx.navigateTo({ url: '/pages/knowledge/index' });
          break;
        case 'my':
          wx.navigateTo({ url: '/pages/my/index' });
          break;
      }
    };
    
    // 获取问卷列表
    const getSurveyList = async () => {
      // 这里是演示数据，实际开发时应该调用接口获取数据
      // const res = await api.getSurveyList();
      // if (res.code === 1) {
      //   surveyList.value = res.data.list || [];
      // }
    };
    
    onMounted(() => {
      getSurveyList();
    });
    
    return {
      surveyList,
      loading,
      loadingText,
      startSurvey,
      handleTabClick
    };
  },
  // 添加微信小程序原生方法定义
  methods: {
    handleTabClick(tab) {
      console.log('handleTabClick called with:', tab);
      switch (tab) {
        case 'home':
          console.log('Navigating to home page');
          uni.navigateTo({ url: '/pages/index/index' });
          break;
        case 'survey':
          // 当前已在问卷页，不需要跳转
          console.log('Already on survey page');
          break;
        case 'record':
          console.log('Navigating to record page');
          uni.navigateTo({ url: '/pages/record/index' });
          break;
        case 'knowledge':
          console.log('Navigating to knowledge page');
          uni.navigateTo({ url: '/pages/knowledge/index' });
          break;
        case 'my':
          console.log('Navigating to my page');
          uni.navigateTo({ url: '/pages/my/index' });
          break;
      }
    }
  }
};
</script>

<style>
.container {
  display: flex;
  flex-direction: column;
  height: 100%;
  background-color: #f5f5f5;
}

.content {
  flex: 1;
  padding: 20rpx;
  padding-bottom: 120rpx;
}

.survey-list {
  padding: 10rpx;
}

.survey-item {
  margin-bottom: 30rpx;
}

.survey-card {
  background-color: #fff;
  border-radius: 16rpx;
  padding: 30rpx;
  box-shadow: 0 2rpx 20rpx rgba(0, 0, 0, 0.08);
}

.survey-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 16rpx;
  display: block;
}

.survey-desc {
  font-size: 28rpx;
  color: #666;
  margin-bottom: 24rpx;
  display: block;
  line-height: 1.5;
}

.survey-meta {
  display: flex;
  align-items: center;
  margin-bottom: 30rpx;
}

.question-count, .time-estimate {
  font-size: 24rpx;
  color: #999;
  margin-right: 20rpx;
}

.survey-actions {
  display: flex;
  justify-content: flex-end;
}

.btn-primary {
  background-color: #2979ff;
  color: #fff;
  font-size: 28rpx;
  padding: 12rpx 30rpx;
  border-radius: 100rpx;
  border: none;
}

.empty-tip {
  text-align: center;
  padding: 100rpx 0;
  color: #999;
  font-size: 28rpx;
}

.loading-more {
  padding: 20rpx 0;
  text-align: center;
}

.loading-content {
  display: flex;
  align-items: center;
  justify-content: center;
}

.loading-text {
  color: #999;
  font-size: 26rpx;
}

.tab-bar {
  display: flex;
  justify-content: space-around;
  align-items: center;
  height: 100rpx;
  background-color: #fff;
  border-top: 1px solid #eee;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
}

.tab-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10rpx 0;
}

.tab-icon {
  font-size: 32rpx;
  color: #666;
  margin-bottom: 8rpx;
  line-height: 1;
}

.tab-text {
  font-size: 20rpx;
  color: #666;
  line-height: 1;
}

.tab-item.active .tab-text {
  color: #2979ff;
}
</style> 